//package com.yuanlun.elasticsearch.controller;
//
//import org.elasticsearch.action.search.SearchRequestBuilder;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.search.suggest.Suggest;
//import org.elasticsearch.search.suggest.SuggestBuilder;
//import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
//import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @ClassName MoviesSuggestController
// * @Description TODO
// * @Author zxw
// * @DATE 2021/4/30 15:53
// * Version 1.0
// **/
//@RestController
//@RequestMapping(value = "/movies-suggest")
//public class MoviesSuggestController {
//    @Resource
//    private ElasticsearchTemplate elasticsearchTemplate;
//
//    public MoviesSuggestController(@Qualifier("elasticsearchTemplate") ElasticsearchTemplate elasticsearchTemplate) {
//        this.elasticsearchTemplate = elasticsearchTemplate;
//    }
//
//    /**
//     *  "title_suggest": {
//     *           "prefix": "bea",
//     *           "completion": {
//     *             "field": "title",
//     *             "skip_duplicates":true,
//     *             "size":10
//     *           }
//     *         }
//     * @param prefix
//     * @return
//     */
//    @GetMapping(value = "/suggest")
//    public List<String> suggest(String prefix){
//        List<String> results=new ArrayList<>();
//        CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder("title")
//                .prefix(prefix)
//                .skipDuplicates(true)
//                .size(10);
//        SuggestBuilder suggestBuilder = new SuggestBuilder().addSuggestion("title_suggest",completionSuggestionBuilder);
//        //构建一个request
//        SearchRequestBuilder requestBuilder=elasticsearchTemplate.getClient()
//                .prepareSearch("movies2")
//                .suggest(suggestBuilder)
//                .setFetchSource(false);
//        //发请求，获取响应
//        SearchResponse response = requestBuilder.get();
//        Suggest suggest = response.getSuggest();
//        Suggest.Suggestion suggestion = suggest.getSuggestion("title_suggest");
//        List<CompletionSuggestion.Entry> entries = suggestion.getEntries();
//        entries.forEach(entry->{
//            List<CompletionSuggestion.Entry.Option> options = entry.getOptions();
//            options.forEach(op->{
//                results.add(op.getText().toString());
//            });
//        });
//        return results;
//    }
//
//}
